package cn.sdhuijin.healthexam.activity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.util.Log;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import cn.sdhuijin.healthexam.DatabaseHelper;
import cn.sdhuijin.healthexam.bean.IndexBean;
import cn.sdhuijin.healthexam.bean.User;
import cn.sdhuijin.healthexam.util.Constants;
import cn.sdhuijin.healthexam.util.ReadWriteProperties;
import cn.sdhuijin.healthexam.util.Util;

public class AbstractActivity extends Activity {

	/**
	 * 根据ID获取Control的值
	 * 
	 * @param id
	 *            编辑框ID
	 * @return 输入的值
	 */
	public String getControlValue(int id) {
		String value = "";
		String name = findViewById(id).getClass().getSimpleName();
		if (name.equals("EditText")) {
			value = ((EditText) this.findViewById(id)).getText().toString()
					.trim();
		}
		if (name.equals("Spinner")) {
			value = ((Spinner) this.findViewById(id)).getSelectedItem()
					.toString().trim();
		}
		return value;
	}

	/**
	 * 根据ID获取CheckBox是否被选中
	 * 
	 * @param id
	 *            checkbox的ID
	 * @return 选中则返回true,否则，返回false
	 */
	public boolean isCheckBoxChecked(int id) {
		return ((CheckBox) this.findViewById(id)).isChecked();
	}

	/**
	 * 根据ID初始化文本编辑框
	 * 
	 * @param editTextId
	 *            编辑框ID
	 * @return 文本编辑框
	 */
	public EditText initEditTextView(int editTextId) {
		return (EditText) this.findViewById(editTextId);
	}

	/**
	 * 提示信息
	 * 
	 * @param informationId
	 *            提示信息ID
	 */
	protected void toastInformation(int informationId) {
		Toast.makeText(this, this.getResources().getString(informationId),
				Toast.LENGTH_SHORT).show();
	}

	/**
	 * 初始化按钮
	 * 
	 * @param buttonId
	 *            编辑ID
	 * @return 按钮
	 */
	public void setButtonListener(OnClickListener listenner, int... buttonId) {
		for (int id : buttonId) {
			Button bt = (Button) this.findViewById(id);
			bt.setOnClickListener(listenner);
		}
	}

	/**
	 * 保存建档纪录到索引表
	 * 
	 * @param createDate
	 *            录入时间
	 * @param identifyNum
	 *            身份证号
	 * @param DataType
	 *            数据类型
	 */
	protected void savaInfoToIndex(String createDate, String identifyNum,
			String DataType) {
		IndexBean indexBean = new IndexBean();
		DatabaseHelper databaseHelper = new DatabaseHelper(this);
		// 身份证号
		indexBean.setIdentifyNum(identifyNum);
		// 数据类型
		indexBean.setDataType(DataType);
		// 录入日期
		indexBean.setCreateDate(createDate);
		// 是否完成标示:否
		indexBean.setCompleteFlag(Constants.COMPLETE_FLAG_NO);
		// 责任医师编号
		indexBean.setDoctorNum(User.getCurrentUser().getName());
		databaseHelper.insertDataToIndex(indexBean);
	}

	/**
	 * 根据用户选择项计算各体质对应转化分
	 * 
	 * @param type
	 *            体质类型
	 * @param radioGroupIds
	 *            用户选择项
	 * @return
	 */
	protected int getBodyTypeTransferScore(String type, int... radioGroupIds) {
		// 原始分
		int originScore = 0;
		// 用户选中条目数
		int selectCount = 0;
		for (int id : radioGroupIds) {
			RadioGroup radioGroup = (RadioGroup) this.findViewById(id);
			if (radioGroup != null) {
				RadioButton radioButton = (RadioButton) this
						.findViewById(radioGroup.getCheckedRadioButtonId());
				if (radioButton != null) {
					String checkedRdbText = radioButton.getText().toString();
					int score = getOriginScoreByCheckRdbText(checkedRdbText);
					originScore += score;
					selectCount++;
				}
			}
		}
		// 转化分数计算：[（原始分-条目数）/（条目数×4）] ×100
		if (selectCount > 0) {
			return ((originScore - selectCount) * 100) / (selectCount * 4);
		}
		return 0;
	}

	private int getOriginScoreByCheckRdbText(String checkedRdbText) {
		int score = 0;
		if ("没有".equals(checkedRdbText)) {
			score += 1;
		} else if ("很少".equals(checkedRdbText)) {
			score += 2;
		} else if ("有时".equals(checkedRdbText)) {
			score += 3;
		} else if ("经常".equals(checkedRdbText)) {
			score += 4;
		} else if ("总是".equals(checkedRdbText)) {
			score += 5;
		}
		return score;
	}

	private int getReverseOriginScoreByCheckRdbText(String checkedRdbText) {
		int score = 0;
		if ("没有".equals(checkedRdbText)) {
			score += 5;
		} else if ("很少".equals(checkedRdbText)) {
			score += 4;
		} else if ("有时".equals(checkedRdbText)) {
			score += 3;
		} else if ("经常".equals(checkedRdbText)) {
			score += 2;
		} else if ("总是".equals(checkedRdbText)) {
			score += 1;
		}
		return score;
	}

	/**
	 * 保存各体质鉴定转化分到文件
	 * 
	 * @param createDate
	 *            文件创建日期
	 * @param score
	 *            转化分
	 * @param type
	 *            体质类型
	 */
	protected void saveBodyJudgeScore(String createDate, String type,
			int... radioGroupIds) {
		int score = getBodyTypeTransferScore(type, radioGroupIds);
		ReadWriteProperties readWriteProperties = new ReadWriteProperties();
		readWriteProperties.storeBodyJudgeProperties(createDate,
				String.valueOf(score), type);
	}

	/**
	 * 保存各种体质鉴定转化分到文件
	 */
	protected void saveBodyJudgeScore(String createDate, String type,
			String... questions) {
		int score = 0;
		if (type.equals("平和质")) {
			score = getPhzTransferScoreFromFile(createDate, questions);
		} else {
			score = getBodyTypeTransferScoreFromFile(createDate, questions);
		}
		Log.e("转化分", "" + score);
		ReadWriteProperties readWriteProperties = new ReadWriteProperties();
		readWriteProperties.storeBodyJudgeProperties(createDate,
				String.valueOf(score), type);
	}

	/**
	 * 根据用户选择项计算各体质对应转化分
	 * 
	 */
	protected int getBodyTypeTransferScoreFromFile(String createDate,
			String... questions) {
		// 原始分
		int originScore = 0;
		// 用户选中条目数
		Properties properties = new Properties();
		try {
			Log.e("文件名", createDate);
			properties.load(new FileInputStream(Constants.PROPERTIES_PATH
					+ createDate + ".txt"));
			for (String str : questions) {
				Log.e("条目", str);
				String result = properties.getProperty(str);
				Log.e("获取存的值", result);
				if (Util.allIsNotEmpty(result)) {
					int score = getOriginScoreByCheckRdbText(result);
					Log.e("得分", score + "");
					originScore += score;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return originScore;
	}

	/**
	 * 根据用户选择项计算平和质对应转化分
	 * 
	 */
	protected int getPhzTransferScoreFromFile(String createDate,
			String... questions) {
		// 原始分
		int originScore = 0;
		// 用户选中条目数
		Properties properties = new Properties();
		try {
			Log.e("文件名", createDate);
			properties.load(new FileInputStream(Constants.PROPERTIES_PATH
					+ createDate + ".txt"));
			for (String str : questions) {
				Log.e("条目", str);
				String result = properties.getProperty(str);
				Log.e("获取存的值", result);
				if (Util.allIsNotEmpty(result)) {
					int score = 0;
					if (str.equals("question01")) {
						score = getOriginScoreByCheckRdbText(result);
					} else {
						score = getReverseOriginScoreByCheckRdbText(result);
					}
					Log.e("得分", score + "");
					originScore += score;
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return originScore;
	}

}
